//
//  PushTypeView.m
//  BGM
//
//  Created by liuqin on 2018/12/3.
//  Copyright © 2018 com.alirtc.demo. All rights reserved.
//

#import "PushTypeView.h"
#import "Common.h"
@implementation PushTypeView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self != 0) {
        UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width,  frame.size.height)];
        bgView.backgroundColor = [UIColor grayColor];
        bgView.alpha = 0.4;
        [self addSubview:bgView];
        
    
    }
    return self;
}
- (void)pushType:(NSArray *)array{
    self.typeArray = array;
    int h =  (int) ceil(self.typeArray.count / 4.0);//向上
    UIView *bg = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, (h)*50)];
    bg.backgroundColor = [UIColor whiteColor];
    [self addSubview:bg];
    
    
    for (int i = 0;  i < array.count; i++) {
        int x = i%4;
        int y = i/4;
        float w =SCREENW/4;
        
        UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(w*x, y*50, w, 50)];
        [self addSubview:bgView];
        
        UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(10,10, w-20, 30)];
        NSDictionary *dic = [array objectAtIndex:i];
        btn.layer.borderColor = bgmsmallColor.CGColor;
        btn.layer.borderWidth = 0.5;
        btn.layer.masksToBounds = YES;
        btn.layer.cornerRadius = 4;
        btn.titleLabel.font = [UIFont systemFontOfSize:12];
        [btn setTitle:[NSString stringWithFormat:@"%@",[dic objectForKey:@"name"]] forState:UIControlStateNormal];
        [btn setTitleColor:bgmmainColor forState:UIControlStateNormal];
        btn.tag = i;
        [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
        [bgView addSubview:btn];
        
    }
}

- (void)clickBtn:(UIButton *)btn{
     [self.delegate clickType:[self.typeArray objectAtIndex:(int)btn.tag]];
}
@end
